package com.qf.controller;

import com.qf.dao.GoodsDao;
import com.qf.entity.Goods;
import com.qf.entity.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;
import jdk.nashorn.internal.objects.annotations.Where;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.controller
 * @Date 2023/4/14 11:36
 */
@WebServlet(urlPatterns = "/goodsservlet")
public class GoodsServlet extends BaseServlet {
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int pn = 1;
        int ps = 8;
        if (!StringUtils.isEmpty(pageNum)) {
            pn = Integer.parseInt(pageNum);
            if (pn < 1) {
                pn = 1;
            }
        }
        if (!StringUtils.isEmpty(pageSize)) {
            ps = Integer.parseInt(pageSize);
            if (ps < 1) {
                ps = 8;
            }
        }
        String typeId = request.getParameter("typeId");
        String name = request.getParameter("name");
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        if (!StringUtils.isEmpty(typeId)) {
            try {
                int id = Integer.parseInt(typeId);
                sb.append("and typeid=?");
                params.add(id);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        if (!StringUtils.isEmpty(name)) {
            sb.append("and name like ?");
            params.add("%" + name + "%");
        }
        if (sb.length() > 0) {
            sb.replace(0, 3, "where");
        } else {
            return "/index.jsp";
        }


        try {
            GoodsService goodsService = new GoodsServiceImpl();
            PageBean<Goods> pageBean = goodsService.selectGoodsByPage(pn, ps, params, sb.toString());
            System.out.println(pageBean.toString());

            request.getSession().setAttribute("pageBean", pageBean);
            request.getSession().setAttribute("typeId", typeId);
            request.getSession().setAttribute("name", name);

            return "/goodsList.jsp";
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "redirect:/index.jsp";
        }

    }

    public String getGoodsById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        try {
            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.getGoodsById(id);

            request.getSession().setAttribute("goods", goods);

            return "/goodsDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/index.jsp";
        }
    }
}

